﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using saveimagetodatabase;
using System.Configuration;
using System.Xml;
using System.IO;
using System.Data.SqlClient;
using System.Data;
using System.ServiceModel;
namespace WcfServiceLibrary2
{
    [DataContract]
   public class Configguration
    {
        public static void SaveConfig(string user,string database,string password,string datasource)
        {
            OpenConnection(user, password, database, datasource);
            FileStream file = new FileStream("Configuration.xml", FileMode.Create);
            XmlTextWriter xml = new XmlTextWriter(file,Encoding.Unicode);
            xml.WriteStartDocument();
            xml.WriteStartElement("config");
            //xml.WriteStartAttribute("vanhung1");
            //xml.WriteString("vanhung1");
            //xml.WriteEndAttribute();
            // xml.WriteString("nguyen van hung dai hoc bach khoa ha noi");
            xml.WriteStartElement("user");
            xml.WriteString(user+";");
            xml.WriteEndElement();

            xml.WriteStartElement("database");
            xml.WriteString(database+";");
            xml.WriteEndElement();
            xml.WriteStartElement("password");
            xml.WriteString("vanhung");
            xml.WriteEndElement();
            xml.WriteStartElement("datasource");
            xml.WriteString(@datasource+";");
            xml.WriteEndElement();
            xml.WriteEndElement();
            xml.WriteEndDocument();
            xml.Close();

        }
        private  static void OpenConnection(string user,string password,string database,string datasource)
        {
           try
           {
               SqlConnection con = new SqlConnection();
               con.ConnectionString = @"Data Source=" + datasource + "Initial Catalog=" + database + "Persist Security Info=True;" + "User ID=" + user + "Password=" + password;
               con.Open();
               if (con.State==ConnectionState.Open)
               {
                   con.Close();
               }
           }
           catch (System.Exception ex)
           {
               throw new FaultException(ex.Message);
           }
        }
    }
}
